package com.fog.student.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fog.student.dto.CourseDto;
import com.fog.student.dto.SelectedCourseDto;
import com.fog.student.entity.Course;
import com.fog.student.entity.SelectedCourse;
import com.fog.student.entity.Student;
import com.fog.student.mapper.SelectedCourseMapper;
import com.fog.student.mapper.StudentMapper;
import com.fog.student.service.CourseService;
import com.fog.student.service.SelectedCourseService;
import com.fog.student.service.StudentService;
import com.fog.student.service.TeacherService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class SelectedCourseServiceImpl extends ServiceImpl<SelectedCourseMapper, SelectedCourse> implements SelectedCourseService {
    @Resource
    private CourseService courseService;

    @Resource
    private StudentService studentService;

    @Resource
    private StudentMapper studentMapper;

    @Override
    @Transactional
    public Page<SelectedCourseDto> pageSearch(int page, int pageSize, String courseName,String beginTime,String endTime) {
        Page<SelectedCourse> pageInfo = new Page<>(page, pageSize);
        // 返回页面的分页
        Page<SelectedCourseDto> selectedCourseDtoPage = new Page<>();

        QueryWrapper<SelectedCourse> selectedCourseQueryWrapper = new QueryWrapper<>();
        QueryWrapper<Course> courseQueryWrapper = new QueryWrapper<>();

        selectedCourseQueryWrapper.orderByDesc("id");
        if(courseName !=null){
            courseQueryWrapper.eq("name",courseName);
            List<Course> courseList =  courseService.list(courseQueryWrapper);
            Set<String> idSet = new HashSet<>();
            for (Course course : courseList){
                String id = course.getId().toString();
                idSet.add(id);
            }
            selectedCourseQueryWrapper.in("course_id",idSet);
        }
        if(beginTime !=null&&endTime!=null){
            selectedCourseQueryWrapper.between("selected_time", beginTime,endTime);
        }
        // 查出信息
        page(pageInfo, selectedCourseQueryWrapper);
        // 拷贝信息
        BeanUtils.copyProperties(pageInfo, selectedCourseDtoPage);

        List<SelectedCourse> records = pageInfo.getRecords();

        List<SelectedCourseDto> recordsDto = records.stream().map((item)->{
            SelectedCourseDto selectedCourseDto = new SelectedCourseDto();
            BeanUtils.copyProperties(item, selectedCourseDto);
            String newCourseName = courseService.getById(item.getCourseId()).getName();
            String studentName = studentService.getById(item.getStudentId()).getName();
            selectedCourseDto.setStudentName(studentName);
            selectedCourseDto.setCourseName(newCourseName);
            return selectedCourseDto;
        }).collect(Collectors.toList());
        // 重新放入到 返回集合中
        selectedCourseDtoPage.setRecords(recordsDto);
        return selectedCourseDtoPage;
    }

    @Override
    public Page<SelectedCourseDto> selectSearch(int page, int pageSize, String name) {
        Page<SelectedCourse> pageInfo = new Page<>(page, pageSize);
        // 返回页面的分页
        Page<SelectedCourseDto> selectedCourseDtoPage = new Page<>();

        QueryWrapper<SelectedCourse> selectedCourseQueryWrapper = new QueryWrapper<>();


        selectedCourseQueryWrapper.orderByDesc("id");
        if(name !=null){
            //  studentQueryWrapper.like("name",name);
            selectedCourseQueryWrapper.like("id", name);
        }
        // 查出信息
        page(pageInfo, selectedCourseQueryWrapper);
        // 拷贝信息
        BeanUtils.copyProperties(pageInfo, selectedCourseDtoPage);

        List<SelectedCourse> records = pageInfo.getRecords();
        List<SelectedCourseDto> recordsDto = records.stream().map((item)->{
            SelectedCourseDto selectedCourseDto = new SelectedCourseDto();
            BeanUtils.copyProperties(item, selectedCourseDto);
            String courseName = courseService.getById(item.getCourseId()).getName();
            String studentName = studentService.getById(item.getStudentId()).getName();
            selectedCourseDto.setStudentName(studentName);
            selectedCourseDto.setCourseName(courseName);
            return selectedCourseDto;
        }).collect(Collectors.toList());
        // 重新放入到 返回集合中
        selectedCourseDtoPage.setRecords(recordsDto);
        return selectedCourseDtoPage;
    }

    @Override
    @Transactional
    public Page<SelectedCourseDto> noSelectSearch(int page, int pageSize, String name) {
        Page<SelectedCourse> pageInfo = new Page<>(page, pageSize);
        // 返回页面的分页
        Page<SelectedCourseDto> selectedCourseDtoPage = new Page<>();

        QueryWrapper<SelectedCourse> selectedCourseQueryWrapper = new QueryWrapper<>();

        QueryWrapper<Student> studentQueryWrapper = new QueryWrapper<>();
        selectedCourseQueryWrapper.orderByDesc("id");
        if(name !=null){
            //  studentQueryWrapper.like("name",name);
            selectedCourseQueryWrapper.like("id", name);
        }

        // 查出信息
        page(pageInfo, selectedCourseQueryWrapper);
        // 拷贝信息
        BeanUtils.copyProperties(pageInfo, selectedCourseDtoPage);

        List<SelectedCourse> records = pageInfo.getRecords();
        List<SelectedCourseDto> recordsDto = records.stream().map((item)->{
            SelectedCourseDto selectedCourseDto = new SelectedCourseDto();
            BeanUtils.copyProperties(item, selectedCourseDto);
            String courseName = courseService.getById(item.getCourseId()).getName();
            String studentName = studentService.getById(item.getStudentId()).getName();
            selectedCourseDto.setStudentName(studentName);
            selectedCourseDto.setCourseName(courseName);
            return selectedCourseDto;
        }).collect(Collectors.toList());
        // 重新放入到 返回集合中
        selectedCourseDtoPage.setRecords(recordsDto);
        return selectedCourseDtoPage;
    }


}
